package com.iyao_app.im.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.iyao_app.R;
import com.iyao_app.base.BaseActivityWithHead;
import com.iyao_app.fragment.FriendFragment;
import com.iyao_app.fragment.InterestMyFragment;
import com.iyao_app.fragment.MyInterestFragment;
import com.iyao_app.util.Contants;
import com.iyao_app.util.ResourcesUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;


public class AttentionActivity extends BaseActivityWithHead implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.friend)
    RadioButton mFriend;
    @BindView(R.id.my_interest)
    RadioButton mMyInterest;
    @BindView(R.id.interest_my)
    RadioButton mInterestMy;
    @BindView(R.id.attention)
    RadioGroup mAttention;
    @BindView(R.id.text_friend)
    TextView mTextFriend;
    @BindView(R.id.text_my_interest)
    TextView mTextMyInterest;
    @BindView(R.id.text_interest_my)
    TextView mTextInterestMy;
    private RadioGroup rg;
    private RadioButton[] rbArray;
    private ArrayList<Fragment> listFragment;
    private FragmentManager fm;
    private int count = 0;
    private int type;
    private String id;
    private Bundle bundle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attention);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initView() {
        type = getIntent().getIntExtra(Contants.TYPE, 0);
        id = getIntent().getStringExtra(Contants.CLASS_TYPE);
        switch (type) {
            case 0:
                mFriend.setText(R.string.friend);
                mMyInterest.setText(R.string.my_interest);
                mInterestMy.setText(R.string.interest_my);
                break;
            case 1:
                mFriend.setText(R.string.friend_ta);
                mMyInterest.setText(R.string.my_interest_ta);
                mInterestMy.setText(R.string.interest_my_ta);
                break;
        }
        fm = getSupportFragmentManager();
        rg = (RadioGroup) findViewById(R.id.attention);
        rbArray = new RadioButton[rg.getChildCount()];
        for (int i = 0; i < rg.getChildCount(); i++) {
            rbArray[i] = (RadioButton) rg.getChildAt(i);
        }
        listFragment = new ArrayList<>();
        if (!TextUtils.isEmpty(id)) {
            bundle = new Bundle();
            bundle.putString(Contants.OTHER_FIELD, id);
        }
        FriendFragment friendFragment = new FriendFragment();
        if (!TextUtils.isEmpty(id))
            friendFragment.setArguments(bundle);
        MyInterestFragment myInterestFragment = new MyInterestFragment();
        if (!TextUtils.isEmpty(id))
            myInterestFragment.setArguments(bundle);
        InterestMyFragment interestMyFragment = new InterestMyFragment();
        if (!TextUtils.isEmpty(id))
            interestMyFragment.setArguments(bundle);
        //  AddressBookFragment addressBookFragment = new AddressBookFragment();
        listFragment.add(friendFragment);
        listFragment.add(myInterestFragment);
        listFragment.add(interestMyFragment);
        // listFragment.add(addressBookFragment);

        FragmentTransaction tt = fm.beginTransaction();
        tt.add(R.id.fragment_attention, listFragment.get(0));
        tt.commit();

        rbArray[0].setChecked(true);
        rg.setOnCheckedChangeListener(this);
    }

    private void initData() {
        setTitleTV(ResourcesUtils.getString(R.string.attention));
    }


    private void showFragment(int id) {
        FragmentTransaction tt = fm.beginTransaction();
        for (int i = 0; i < rg.getChildCount(); i++) {
            if (rbArray[i].getId() == id) {
                if (listFragment.get(i).isAdded()) {
                    tt.show(listFragment.get(i)).hide(listFragment.get(count)).commit();
                } else {
                    tt.add(R.id.fragment_attention, listFragment.get(i)).hide(listFragment.get(count)).commit();
                }
                count = i;
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.friend:
                showFragment(i);
                mFriend.setTextColor(getResources().getColor(R.color.inform_oval));
                mMyInterest.setTextColor(getResources().getColor(R.color.app_gray_1));
                mInterestMy.setTextColor(getResources().getColor(R.color.app_gray_1));
                //  mAddressBook.setTextColor(getResources().getColor(R.color.app_gray_1));
                mTextFriend.setVisibility(View.VISIBLE);
                mTextMyInterest.setVisibility(View.GONE);
                mTextInterestMy.setVisibility(View.GONE);
                //  mTextAddressBook.setVisibility(View.GONE);
                break;
            case R.id.my_interest:
                showFragment(i);
                mFriend.setTextColor(getResources().getColor(R.color.app_gray_1));
                mMyInterest.setTextColor(getResources().getColor(R.color.inform_oval));
                mInterestMy.setTextColor(getResources().getColor(R.color.app_gray_1));
                mTextFriend.setVisibility(View.GONE);
                mTextMyInterest.setVisibility(View.VISIBLE);
                mTextInterestMy.setVisibility(View.GONE);
                break;
            case R.id.interest_my:
                showFragment(i);
                mFriend.setTextColor(getResources().getColor(R.color.app_gray_1));
                mMyInterest.setTextColor(getResources().getColor(R.color.app_gray_1));
                mInterestMy.setTextColor(getResources().getColor(R.color.inform_oval));
                mTextFriend.setVisibility(View.GONE);
                mTextMyInterest.setVisibility(View.GONE);
                mTextInterestMy.setVisibility(View.VISIBLE);
                break;
        }
    }
}